{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "##################### 机器学习——线性回归分析 #####################\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.metrics import r2_score #评价预测模型的性能\n",
    "import matplotlib.pyplot as plt #画图\n",
    "import seaborn as sns\n",
    "\n",
    "# 图形在Notebook显示\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PTRATIO</th>\n",
       "      <th>...</th>\n",
       "      <th>RAD_2</th>\n",
       "      <th>RAD_3</th>\n",
       "      <th>RAD_4</th>\n",
       "      <th>RAD_5</th>\n",
       "      <th>RAD_6</th>\n",
       "      <th>RAD_7</th>\n",
       "      <th>RAD_8</th>\n",
       "      <th>RAD_24</th>\n",
       "      <th>MEDV</th>\n",
       "      <th>log_MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.058148</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.314815</td>\n",
       "      <td>0.577505</td>\n",
       "      <td>0.641607</td>\n",
       "      <td>0.268711</td>\n",
       "      <td>0.208015</td>\n",
       "      <td>0.3</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.433790</td>\n",
       "      <td>0.674359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.234444</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.547998</td>\n",
       "      <td>0.782698</td>\n",
       "      <td>0.348524</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.378995</td>\n",
       "      <td>0.626668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000236</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.234444</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.172840</td>\n",
       "      <td>0.694386</td>\n",
       "      <td>0.599382</td>\n",
       "      <td>0.348524</td>\n",
       "      <td>0.104962</td>\n",
       "      <td>0.5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.678082</td>\n",
       "      <td>0.842711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000293</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.053333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.658555</td>\n",
       "      <td>0.441813</td>\n",
       "      <td>0.448173</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.648402</td>\n",
       "      <td>0.825183</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000705</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.053333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.150206</td>\n",
       "      <td>0.687105</td>\n",
       "      <td>0.528321</td>\n",
       "      <td>0.448173</td>\n",
       "      <td>0.066794</td>\n",
       "      <td>0.6</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.712329</td>\n",
       "      <td>0.862159</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       CRIM    ZN     INDUS  CHAS       NOX        RM       AGE       DIS  \\\n",
       "0  0.000000  0.18  0.058148   0.0  0.314815  0.577505  0.641607  0.268711   \n",
       "1  0.000236  0.00  0.234444   0.0  0.172840  0.547998  0.782698  0.348524   \n",
       "2  0.000236  0.00  0.234444   0.0  0.172840  0.694386  0.599382  0.348524   \n",
       "3  0.000293  0.00  0.053333   0.0  0.150206  0.658555  0.441813  0.448173   \n",
       "4  0.000705  0.00  0.053333   0.0  0.150206  0.687105  0.528321  0.448173   \n",
       "\n",
       "        TAX  PTRATIO  ...  RAD_2  RAD_3  RAD_4  RAD_5  RAD_6  RAD_7  RAD_8  \\\n",
       "0  0.208015      0.3  ...      0      0      0      0      0      0      0   \n",
       "1  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "2  0.104962      0.5  ...      1      0      0      0      0      0      0   \n",
       "3  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "4  0.066794      0.6  ...      0      1      0      0      0      0      0   \n",
       "\n",
       "   RAD_24      MEDV  log_MEDV  \n",
       "0       0  0.433790  0.674359  \n",
       "1       0  0.378995  0.626668  \n",
       "2       0  0.678082  0.842711  \n",
       "3       0  0.648402  0.825183  \n",
       "4       0  0.712329  0.862159  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入特征工程预处理的数据\n",
    "df = pd.read_csv(r'E:/BostonData/BostonHousePrice_CodeData/test01.csv')\n",
    "\n",
    "# 显示数据前5行内容\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从原始数据中分离输入特征x和输出y\n",
    "y = df[\"MEDV\"]\n",
    "\n",
    "X = df.drop([\"MEDV\", \"log_MEDV\"], axis = 1)\n",
    "\n",
    "#特征名称，用于后续显示权重系数对应的特征\n",
    "feat_names = X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(392, 21)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据分割工具包\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 数据的20%作为测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2) \n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>0.446699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>0.104658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>0.061897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>0.041508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>0.039532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>0.033091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>0.021226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>0.008079</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.011200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.013261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-0.020636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>-0.024599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.041786</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-0.054486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-0.056163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-0.109003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-0.114821</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-0.180497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-0.191897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-0.331288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-0.345060</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns      coef\n",
       "5        RM  0.446699\n",
       "1        ZN  0.104658\n",
       "20   RAD_24  0.061897\n",
       "10        B  0.041508\n",
       "19    RAD_8  0.039532\n",
       "18    RAD_7  0.033091\n",
       "14    RAD_3  0.021226\n",
       "3      CHAS  0.008079\n",
       "16    RAD_5 -0.011200\n",
       "15    RAD_4 -0.013261\n",
       "13    RAD_2 -0.020636\n",
       "2     INDUS -0.024599\n",
       "6       AGE -0.041786\n",
       "12    RAD_1 -0.054486\n",
       "17    RAD_6 -0.056163\n",
       "8       TAX -0.109003\n",
       "4       NOX -0.114821\n",
       "9   PTRATIO -0.180497\n",
       "0      CRIM -0.191897\n",
       "7       DIS -0.331288\n",
       "11    LSTAT -0.345060"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "######################################## 最小二乘线性回归 ########################################\n",
    "\n",
    "#class sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "# 1.使用默认配置初始化学习器实例\n",
    "lr = LinearRegression()\n",
    "\n",
    "# 2.用训练数据训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "# 3. 用训练好的模型对测试集进行预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "\n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.6918346703206382\n",
      "The r2 score of LinearRegression on train is 0.8001507257439991\n"
     ]
    }
   ],
   "source": [
    "#  训练模型评估\n",
    "# 测试集\n",
    "print('The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))\n",
    "# 训练集\n",
    "print('The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x1e518620b08>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeUAAAFsCAYAAADyj6FyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAbK0lEQVR4nO3df7jWdZ3n8edbOEIjAaGgCG1gaSkqYAeCbVSKSmcwca9tssYfeInaWs06tf2yH+PJqZ1aXZvcnK7cfqhpI+pOo1tXuzYEQ7qmA8aYSgWVTCDhkVIENfnx3j/uL3SAczj3OZxz7g/nfj6u61zn/v64v9/3/bkPvO7P98fnjsxEkiQ13iGNLkCSJNUYypIkFcJQliSpEIayJEmFMJQlSSqEoSxJUiEMZTW9iHgsIuY0uo5Gioj/EBG/jogtETF9APe7JSKO6WLZRRFxXx/t54mIeEtfbEvqT4ayBrXO/jPe+z/7zJySmUu72c6kiMiIGNpPpTbatcD7M3NEZv5474XVa99ahej6iLguIoYc6E6r/f3yQLcjDRaGslSAAsL+VcBj3awzNTNHAKcD5wIX93tVUpMxlNX0OvamI2JmRCyPiM0RsTEirqtWW1b9fqbqLc6OiEMi4pMRsTYinoqIWyJiVIftXlgt2xQRn9prP20RcVdE3BoRm4GLqn0/EBHPRMSGiPhSRBzaYXsZEe+NiNUR8VxE/HVEvLp6zuaIuKPj+nu9xk5rjYhhEbEFGAL8a0T8orv2ysw1wP3AtA7bHxURX6vqXh8Rn9nVk46I10TEP0fEsxHxdEQs2us1vaZ6fHhE3FO9loeAV3dYb58jFRGxNCIuqR6/OiJ+ULX10xFxW0SM7qItunqPpYYzlKU9fRH4YmaOpBYKd1TzT6t+j64OuT4AXFT9vAk4BhgBfAkgIk4A/g44DxgPjAIm7LWv+cBdwGjgNmAH8AHgCGA2MBd4717PORN4PTAL+AhwY7WPVwInAu/u4nV1Wmtm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8/x2te+lssvv5w1a9awaNEi7r//flpaWnjve9/LbbfdxoUXXshnP/tZxowZw44dO5g7dy6PPPIIJ5988u6677vvvl7V0ZGhLEkC4IUXXmDatGk88cQTvP71r+etb30rUAvle++9d3egbtmyhdWrV3PqqafyoQ99iI9+9KOcddZZnHrqqXts78EHH2TOnDmMHVv7QqRzzz2Xn//85/utITP5+Mc/zrJlyzjkkENYv349Gzdu5Kijjtq9zowZM7j44ovZtm0b55xzDtOmTevV6503bx7Dhg1j2LBhjBs3jo0bN7J48WJWrFjBjBkzdrfJuHHjALjjjju48cYb2b59Oxs2bODxxx/fHcrnnntur2rYm4evJUnAH84pr127lpdeemn3OeXM5Morr2TlypWsXLmSNWvWsHDhQo477jhWrFjBSSedxJVXXsnVV1+9zza7ui1o6NCh7Ny5c/f2X3rpJQBuu+022tvbWbFiBStXruTII4/c517f0047jWXLljFhwgQuuOACbrnlll693mHDhu1+PGTIELZv305msmDBgt2v9Wc/+xltbW386le/4tprr2Xx4sU88sgjzJs3b4+6DjvssF7VsDd7ympa9V7d2tdXwUqlGzVqFNdffz3z58/n8ssv54wzzuBTn/oU5513HiNGjGD9+vW0tLSwfft2xowZw/nnn8+IESO46aab9tjOG97wBq644go2bdrEyJEjufPOO5k6dSpQOw+7YsUK3vnOd3L33Xezbds2oHYIfNy4cbS0tLBkyRLWrt33Ww7Xrl3LhAkTuPTSS9m6dSsPP/wwF154YZ+89rlz5zJ//nw+8IEPMG7cOH7729/y3HPPsXnzZg477DBGjRrFxo0b+d73vsecOXP6ZJ8dGcqSVKBGfxicPn06U6dO5fbbb+eCCy5g1apVzJ49G6hdlHXrrbeyZs0aPvzhD3PIIYfQ0tLCl7/85T22MX78eNra2pg9ezbjx4/nlFNO2X3R1KWXXsr8+fOZOXMmc+fO3d3TPO+883j7299Oa2sr06ZN43Wve90+tS1dupRrrrmGlpYWRowY0WVPed68ebvHnZ49ezbve9/7un3dJ5xwAp/5zGd429vexs6dO2lpaeGGG25g1qxZTJ8+nSl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+G8DW2Vj1dlG38rMkzJzGrX2uW6Ay+w3zRTK84Gbq8c3A+fsvUJm/jwzV1ePnwSeAjoddWWQ6raNADJzMfDcQBVVgHqGlO3YdncBcyOiswF2Bqtu2ygzn8jMR4CdjSiwEPW005LMfL6a/BG1MSCaST1ttLnD5GF0MnDVwaqZQvnIzNwAUP0et7+VI2ImcCjwiwGorRQ9aqMmMgH4dYfpddW8TtfJzO3As8DhA1JdGeppI/W8nRYC3+vXispTVxtFxPsi4hfUesr/eYBq63cHMsxmcSLin4CjOln0iR5uZzzwTWBBZg6qT/V91UZNpp4hZesadnYQa/bXX6+62ykizgdagdP7taLy1DuE8w3ADRHx58AngQX9XdhAGFShnJlv6WpZRGyMiPGZuaEK3ae6WG8k8F3gk5n5o34qtWH6oo2aUD1Dyu5aZ11EDAVGAb8dmPKK4LC79amrnSLiLdQ+KJ+emb8foNpK0dO/pduBL/drRQOomQ5f38MfPkktAO7ee4VquNBvA7dk5p0DWFspum2jJlXPkLId2+4dwA+yuUbmcdjd+nTbThExHfgKcHZmNuMH43ra6NgOk/OA1QNYX//KzKb4oXZ+bzG1N28xMKaa3wp8tXp8PrANWNnhZ1qjay+pjarpHwLtwAvUPtWe0ejaB6Bt/hT4ObVrDD5Rzbua2n+cAMOBO4E1wEPAMY2uucA2mlH9vWwFNgGPNbrmQtvpn4CNHf4PuqfRNRfYRl8EHqvaZwkwpdE199WPw2xKklSIZjp8LUlS0QxlSZIKYShLklQIQ1mSpEIYypIkFcJQliSpEIayJEmF+P8ExzLvmyMFTQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声\n",
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5)\n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAeOUlEQVR4nO3de3hU1b3/8feXkJAg0SDiBeRq8YZAwHBHDipioKKItFIivwMIoZyiYn0qij6HgrWKt4LogYpUKQelKjeVu7TcCtEEAwTlIoqpXDzcYyCBJMz398dMaMRcJsns2TOT7+t55snMZLLXRwhf1157rbVFVTHGGCfUcjuAMSZyWYExxjjGCowxxjFWYIwxjrECY4xxTG23A1TGZZddps2bN3c7hjHmAlu2bDmqqg0vfD+sCkzz5s3JyMhwO4Yx5gIikl3a+3aKZIxxjBUYY4xjrMAYYxzjWoERkVgR+UxEtonIFyIyya0sxhhnuDnIexa4TVVPiUg0sFFElqtqmouZjDEB5FoPRr1O+V5G+x628tIYl504cYLHHnuM06dPV/tYro7BiEiUiGwFDgOrVfXTUj6TKiIZIpJx5MiR4Ic0pgbZtWsXbdu25dVXX2XDhg3VPp6rBUZVz6lqInA10ElEbirlM2+oapKqJjVs+JN5PMaYAGrevDmdO3dm8+bNJCcnV/t4IXEVSVVPAmuB6v8XGWMqZdeuXQwcOJAffviB2NhYPvjgA5KSkgJybDevIjUUkQTf8zigN7DLrTzG1DSqysyZM+nQoQPr169n9+7dAW/DzR7MVcA/RGQ7kI53DOZjF/MYU2McPnyYe+65hzFjxnDLLbeQlZVFx44dA96Oa5epVXU70N6t9o2pycaOHcuqVauYOnUqDz30ELVqOdPXkHDakzcpKUltsaMxVZOfn09eXh4NGjTgu+++4+TJk7Rp0yYgxxaRLar6k4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kDelPFfz8RO5NXRBbgvVpyfltOVT3gUpa9QB3gmO+tNFV1ZZtQEbkXmA40BE4CW1X1ziBn6AdMxXtF6S+q+mww2y+R412gF95tCf4PmKiqs13K0gPYAGTh/Z0FmKCqy1zI0haYg/fvpxbwnqpOrtKxIrXAGGPcF7GnSMYY91mBMcY4xgqMMcYxVmCMMY6xAmOMcUxYT7QzzhCRBsAa38srgXPAEd/rTr41RMHOtBIY5FunY8KEXaY25RKR3wOnVPWlC94XvL8/nlJ/MHDtB6Ud4ww7RTJ+E5Gf+dY0zQQ+B5qIyMkS3x8sIm/6nl8hIgtFJMO3t0iXUo430rc/zkrf/jBPl9HOVSKyv8Ts0uG+vXW2ichb/rZngs9OkUxl3QgMV9Vfi0h5vz+vAi+oappvdfDHQGkL5jr53i8A0kXkY+BUyXYAvB0ZEJF2eJdddFPV4yJyaSXbM0FkBcZU1teqmu7H53oD1xUXBqC+iMSpav4Fn1upqicARGQx0ANYUU47twF/U9XjAMVfK9GeCSIrMKayTpd47uHH2y/Elngu+DcgfOEgYPHr0xd+sMRxSxs49Lc9E0Q2BmOqzDfwekJEWolILeDeEt/+BPhN8QsRSSzjMH1EJEFE6uLd2e6fFTT7CTC4+NSoxCmSv+2ZILICY6prPN5TmjV493op9hugu28w9ktgVBk/vxHvfj2ZwLuqurW8xlR1O/ACsN6349qLlWzPBJFdpjauEZGRwE2qOs7tLMYZ1oMxxjjGejDGGMdYD8YY4xgrMMYYx1iBMcY4xgqMMcYxVmCMMY75//RyY8WKOaPnAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 观察预测值与真值的散点图\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True price')\n",
    "plt.ylabel('Predicted price')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.6932533010598798\n",
      "The r2 score of RidgeCV on train is 0.800079018611284\n"
     ]
    }
   ],
   "source": [
    "######################################### 岭回归／L2正则 #########################################\n",
    "\n",
    "#class sklearn.linear_model.RidgeCV(alphas=(0.1, 1.0, 10.0), fit_intercept=True, \n",
    "#                                  normalize=False, scoring=None, cv=None, gcv_mode=None, \n",
    "#                                  store_cv_values=False)\n",
    "from sklearn.linear_model import  RidgeCV\n",
    "\n",
    "#1. 设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "#n_alphas = 20\n",
    "#alphas = np.logspace(-5,2,n_alphas)\n",
    "\n",
    "#2. 生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))\n",
    "print('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEGCAYAAABy53LJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3de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cDvtAYWGhEE9DhERSYoejouISFIUHCIikhQFh4iIJEXBISIiSVFwiIhIUv4/mcg7GqWkfOIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 0.1\n"
     ]
    }
   ],
   "source": [
    "# 训练结果可视化\n",
    "mse_mean = np.mean(ridge.cv_values_, axis = 0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas),1)) \n",
    "\n",
    "#这是为了标出最佳参数的位置，不是必须\n",
    "#plt.plot(np.log10(ridge.alpha_)*np.ones(3), [0.28, 0.29, 0.30])\n",
    "\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()\n",
    "\n",
    "print('alpha is:', ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>0.446699</td>\n",
       "      <td>0.440948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>0.104658</td>\n",
       "      <td>0.101971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>0.061897</td>\n",
       "      <td>0.058989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>0.041508</td>\n",
       "      <td>0.041514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>0.039532</td>\n",
       "      <td>0.039682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>0.033091</td>\n",
       "      <td>0.032474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>0.021226</td>\n",
       "      <td>0.021768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>0.008079</td>\n",
       "      <td>0.008555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.011200</td>\n",
       "      <td>-0.010714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.013261</td>\n",
       "      <td>-0.013318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-0.020636</td>\n",
       "      <td>-0.018895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>-0.024599</td>\n",
       "      <td>-0.025797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.041786</td>\n",
       "      <td>-0.041070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-0.054486</td>\n",
       "      <td>-0.054392</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-0.056163</td>\n",
       "      <td>-0.055594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-0.109003</td>\n",
       "      <td>-0.106944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-0.114821</td>\n",
       "      <td>-0.110663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-0.180497</td>\n",
       "      <td>-0.179602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-0.191897</td>\n",
       "      <td>-0.178385</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-0.331288</td>\n",
       "      <td>-0.321298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-0.345060</td>\n",
       "      <td>-0.345639</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns   coef_lr  coef_ridge\n",
       "5        RM  0.446699    0.440948\n",
       "1        ZN  0.104658    0.101971\n",
       "20   RAD_24  0.061897    0.058989\n",
       "10        B  0.041508    0.041514\n",
       "19    RAD_8  0.039532    0.039682\n",
       "18    RAD_7  0.033091    0.032474\n",
       "14    RAD_3  0.021226    0.021768\n",
       "3      CHAS  0.008079    0.008555\n",
       "16    RAD_5 -0.011200   -0.010714\n",
       "15    RAD_4 -0.013261   -0.013318\n",
       "13    RAD_2 -0.020636   -0.018895\n",
       "2     INDUS -0.024599   -0.025797\n",
       "6       AGE -0.041786   -0.041070\n",
       "12    RAD_1 -0.054486   -0.054392\n",
       "17    RAD_6 -0.056163   -0.055594\n",
       "8       TAX -0.109003   -0.106944\n",
       "4       NOX -0.114821   -0.110663\n",
       "9   PTRATIO -0.180497   -0.179602\n",
       "0      CRIM -0.191897   -0.178385\n",
       "7       DIS -0.331288   -0.321298\n",
       "11    LSTAT -0.345060   -0.345639"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.6904160529435242\n",
      "The r2 score of LassoCV on train is 0.8001054738751575\n"
     ]
    }
   ],
   "source": [
    "######################################## Lasso／L1正则 ########################################\n",
    "\n",
    "# class sklearn.linear_model.LassoCV(eps=0.001, n_alphas=100, alphas=None, fit_intercept=True, \n",
    "#                                    normalize=False, precompute=’auto’, max_iter=1000, \n",
    "#                                    tol=0.0001, copy_X=True, cv=None, verbose=False, n_jobs=1,\n",
    "#                                    positive=False, random_state=None, selection=’cyclic’)\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#2. 生成学习器实例\n",
    "#lasso = LassoCV(alphas=alphas)\n",
    "\n",
    "#1. 设置超参数搜索范围\n",
    "#Lasso可以自动确定最大的alpha，所以另一种设置alpha的方式是设置最小的alpha值（eps） 和 超参数的数目（n_alphas），\n",
    "#然后LassoCV对最小值和最大值之间在log域上均匀取值n_alphas个\n",
    "# np.logspace(np.log10(alpha_max * eps), np.log10(alpha_max),num=n_alphas)[::-1]\n",
    "\n",
    "#2 生成LassoCV实例（默认超参数搜索范围）\n",
    "lasso = LassoCV()  \n",
    "\n",
    "#3. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#4. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY4AAAEGCAYAAABy53LJAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAgAElEQVR4nO3deXQc5Znv8e+j3Yu8SsbGspGNjY2NMYswIey7GSCGBA8kTMIhZEgyl5lJ5iQZCGOSS5jcwyWZ3CHJZEImCyEhkDCQOGDGLIYQIBjLeJFXLO+SF0leZEu21n7uH10yjdDSbdSq7tbvc04fVVe/Vf28Krt/qreWNndHREQkXllhFyAiIulFwSEiIglRcIiISEIUHCIikhAFh4iIJCQn7AL6Q1FRkZeWloZdhohI2igqKmLx4sWL3X1u59cGRHCUlpZSXl4edhkiImnFzIq6mq+hKhERSYiCQ0REEqLgEBGRhCg4REQkIQoOERFJiIJDREQSouAQEZGEKDhERDLQhj2H+H8vvcvRlvY+X7eCQ0Qkw7g733p2HT9/YxvNbQoOERHpxcvra3ijch9fumIqIwbn9fn6FRwiIhmkpS3Cvy5az+TiIfzNR05KynsoOEREMshjb21na10jC66dQW52cj7iFRwiIhlif2ML//7Su1w4tYhLph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GRhCg4REQkIQoOERFJiIJDpAdm1vAhl38quLq9pzav9nR303jbdGpfbGb/E297kUQoOESSxMxmAtnuvqW/39vda4HdZnZ+f7+3ZD4Fh0gcLOohM1sTfLfBzcH8LDP7j+B7EZ41s0VmdlOw2K3AH2LW8aPgxoxrzex/d/M+DWb2XTN7x8xeNrPimJfnm9nbZvaumV0YtC81sz8H7d8xs4/GtP99UINIn1JwiMTn48AZwGzgCuAhMxsXzC8FZgGfA86LWeZ8YHnM83vdvQw4HbjYzE7v4n2GAO+4+1nAn3j/va1y3H0O8KWY+TXAlUH7m4GHY9qXAxcm3lWRnmX0TQ5F+tAFwG/cvR3Ya2Z/As4J5v/O3SPAHjN7JWaZcUBtzPO/Dm5rnRO8NoPo7UhiRYAng+lfAU/HvNYxvZxoWAHkAj8wszOAduCUmPY1RG8/IdKnFBwi8enqVts9zQc4ChQAmNkk4CvAOe5+wMx+0fFaL2LvCdQc/Gznvf+7XyZ6n6LZREcQmmLaFwQ1iPQpDVWJxOc14GYzyw6OO1wEvA28DnwiONZxAu+/Rfp6YEowPQxoBOqDdtd08z5ZQMcxkk8F6+/JcGB3sMfzaaJfe9rhFFLwDrWS/rTHIRKfZ4gev1hFdC/ga+6+x8z+G7ic6Af0u8BSoD5Y5jmiQfKSu68ysxVE71C6BXijm/dpBGaa2fJgPTf3Utd/AP9tZvOBV4LlO1wa1CDSp3R3XJEPycyGunuDmY0muhdyfhAqg4h+mJ8fHBuJZ10N7j60j+p6DZjn0e8yF+kz2uMQ+fCeNbMRQB7wLQ++RMvdj5rZN4h+X/WO/iwoGE77N4WGJIP2OEREJCE6OC4iIglRcIiISEIUHCIikhAFh4iIJETBISIiCfn/R0j4i+ws+NQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha is: 3.583794696939444e-05\n"
     ]
    }
   ],
   "source": [
    "# 模型可视化\n",
    "mses = np.mean(lasso.mse_path_, axis = 1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses) \n",
    "#plt.plot(np.log10(lasso.alphas_)*np.ones(3), [0.3, 0.4, 1.0])\n",
    "plt.xlabel('log(alpha)')\n",
    "plt.ylabel('mse')\n",
    "plt.show()    \n",
    "            \n",
    "print('alpha is:', lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>RM</td>\n",
       "      <td>0.446699</td>\n",
       "      <td>0.440948</td>\n",
       "      <td>0.447086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ZN</td>\n",
       "      <td>0.104658</td>\n",
       "      <td>0.101971</td>\n",
       "      <td>0.101840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>RAD_24</td>\n",
       "      <td>0.061897</td>\n",
       "      <td>0.058989</td>\n",
       "      <td>0.068733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>B</td>\n",
       "      <td>0.041508</td>\n",
       "      <td>0.041514</td>\n",
       "      <td>0.040645</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>RAD_8</td>\n",
       "      <td>0.039532</td>\n",
       "      <td>0.039682</td>\n",
       "      <td>0.049612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>RAD_7</td>\n",
       "      <td>0.033091</td>\n",
       "      <td>0.032474</td>\n",
       "      <td>0.042680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RAD_3</td>\n",
       "      <td>0.021226</td>\n",
       "      <td>0.021768</td>\n",
       "      <td>0.032456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CHAS</td>\n",
       "      <td>0.008079</td>\n",
       "      <td>0.008555</td>\n",
       "      <td>0.007995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>RAD_5</td>\n",
       "      <td>-0.011200</td>\n",
       "      <td>-0.010714</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>RAD_4</td>\n",
       "      <td>-0.013261</td>\n",
       "      <td>-0.013318</td>\n",
       "      <td>-0.002180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>RAD_2</td>\n",
       "      <td>-0.020636</td>\n",
       "      <td>-0.018895</td>\n",
       "      <td>-0.007915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>INDUS</td>\n",
       "      <td>-0.024599</td>\n",
       "      <td>-0.025797</td>\n",
       "      <td>-0.024521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>AGE</td>\n",
       "      <td>-0.041786</td>\n",
       "      <td>-0.041070</td>\n",
       "      <td>-0.040877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>RAD_1</td>\n",
       "      <td>-0.054486</td>\n",
       "      <td>-0.054392</td>\n",
       "      <td>-0.042503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>RAD_6</td>\n",
       "      <td>-0.056163</td>\n",
       "      <td>-0.055594</td>\n",
       "      <td>-0.044259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>TAX</td>\n",
       "      <td>-0.109003</td>\n",
       "      <td>-0.106944</td>\n",
       "      <td>-0.105925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NOX</td>\n",
       "      <td>-0.114821</td>\n",
       "      <td>-0.110663</td>\n",
       "      <td>-0.111381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PTRATIO</td>\n",
       "      <td>-0.180497</td>\n",
       "      <td>-0.179602</td>\n",
       "      <td>-0.179300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CRIM</td>\n",
       "      <td>-0.191897</td>\n",
       "      <td>-0.178385</td>\n",
       "      <td>-0.178231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>DIS</td>\n",
       "      <td>-0.331288</td>\n",
       "      <td>-0.321298</td>\n",
       "      <td>-0.323374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>LSTAT</td>\n",
       "      <td>-0.345060</td>\n",
       "      <td>-0.345639</td>\n",
       "      <td>-0.346723</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    columns   coef_lr  coef_ridge  coef_lasso\n",
       "5        RM  0.446699    0.440948    0.447086\n",
       "1        ZN  0.104658    0.101971    0.101840\n",
       "20   RAD_24  0.061897    0.058989    0.068733\n",
       "10        B  0.041508    0.041514    0.040645\n",
       "19    RAD_8  0.039532    0.039682    0.049612\n",
       "18    RAD_7  0.033091    0.032474    0.042680\n",
       "14    RAD_3  0.021226    0.021768    0.032456\n",
       "3      CHAS  0.008079    0.008555    0.007995\n",
       "16    RAD_5 -0.011200   -0.010714   -0.000000\n",
       "15    RAD_4 -0.013261   -0.013318   -0.002180\n",
       "13    RAD_2 -0.020636   -0.018895   -0.007915\n",
       "2     INDUS -0.024599   -0.025797   -0.024521\n",
       "6       AGE -0.041786   -0.041070   -0.040877\n",
       "12    RAD_1 -0.054486   -0.054392   -0.042503\n",
       "17    RAD_6 -0.056163   -0.055594   -0.044259\n",
       "8       TAX -0.109003   -0.106944   -0.105925\n",
       "4       NOX -0.114821   -0.110663   -0.111381\n",
       "9   PTRATIO -0.180497   -0.179602   -0.179300\n",
       "0      CRIM -0.191897   -0.178385   -0.178231\n",
       "7       DIS -0.331288   -0.321298   -0.323374\n",
       "11    LSTAT -0.345060   -0.345639   -0.346723"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
